<template>
  <el-main>
    <el-card class="box-card" shadow="never">
      <div class="search-box">
        <el-form :inline="true" :model="searchForm" class="demo-form-inline">
          <el-form-item label="单位名称">
            <el-input
              v-model="searchForm.name"
              placeholder="请输入单位名称"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit" icon="el-icon-search"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div
        style="
          width: 100%;
          height: 0;
          border-bottom: #e4e7ed 1px dashed;
          margin-bottom: 10px;
        "
      ></div>
      <div class="button-group">
        <el-button type="primary" icon="el-icon-plus" @click="addUnit"
          >添加单位</el-button
        >
      </div>
      <div
        style="
          width: 100%;
          height: 0;
          border-bottom: #e4e7ed 1px dashed;
          margin-top: 10px;
        "
      ></div>
      <el-table
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" width="80"> </el-table-column>
        <el-table-column prop="name" label="单位名称" min-width="120">
        </el-table-column>
        <el-table-column label="是否允许小数" width="120">
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.is_float == 1">是</el-tag>
            <el-tag type="info" v-if="scope.row.is_float == 2">否</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" min-width="150">
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间" width="160">
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template #default="scope">
            <el-button @click="handleEdit(scope.row)" type="text" size="small"
              >编辑</el-button
            >
            <el-button @click="handleDel(scope.row)" type="text" size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <Pages
        :form="searchForm"
        :page-obj="page"
        @pageChange="handlePageChange"
        @pageJump="getList"
      ></Pages>
    </el-card>

    <save-dialog
      v-if="dialogVisible"
      ref="saveDialog"
      @success="handleSuccess"
      @closed="dialogVisible = false"
      :close-on-click-modal="false"
    ></save-dialog>
  </el-main>
</template>

<script>
import saveDialog from "@/views/unit/save.vue";
import Pages from "@/components/pages/index.vue";

export default {
  name: "unit",
  components: {
    Pages,
    saveDialog,
  },
  data() {
    return {
      searchForm: {
        name: "",
        limit: 15,
        page: 1,
      },
      dialogVisible: false,
      page: {
        total: 0,
      },
      tableData: [],
      selectData: [],
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 获取列表
    async getList() {
      let res = await this.$API.unit.list.get(this.searchForm);
      if (res.code == 0) {
        this.tableData = res.data.data;
        this.page.total = res.data.total;
      }
    },
    // 搜索
    onSubmit() {
      this.searchForm.page = 1;
      this.getList();
    },
    // 添加单位
    addUnit() {
      this.title = "添加单位";
      this.dialogVisible = true;

      this.$nextTick(() => {
        this.$refs.saveDialog.open("add");
      });
    },
    // 编辑单位
    handleEdit(item) {
      this.title = "编辑单位";
      this.dialogVisible = true;

      this.$nextTick(() => {
        this.$refs.saveDialog.open("edit").setData(item);
      });
    },
    // 删除单位
    handleDel(item) {
      this.$confirm("此操作将永久删除该单位，是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await this.$API.unit.del.get({ id: item.id });
          if (res.code == 0) {
            this.$message.success(res.msg);
            this.getList();
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch(() => {});
    },
    // 分页
    handlePageChange(page) {
      this.searchForm.page = page;
      this.getList();
    },
    handleSuccess() {
      this.dialogVisible = false;
      this.getList();
    },
  },
};
</script>

<style scoped>
.button-group {
  margin-top: 10px;
}
</style>
